package ge.devfest.hackathon.gg;

import java.util.List;

import ge.devfest.hackathon.gg.model.Activity;
import ge.devfest.hackathon.gg.model.Activity.Status;
import ge.devfest.hackathon.gg.utils.IBaseActivityFragment;
import ge.devfest.hackathon.gg.utils.SimpleTabListener;
import ge.devfest.hackathon.gg.utils.ViewPager;
import ge.devfest.hackathon.gg.utils.ViewPager.SimpleOnPageChangeListener;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.WindowManager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.j256.ormlite.dao.RuntimeExceptionDao;

public class ActivityActivity extends BaseActivity {

	private static final String ACTIVITY_ID = "activity_id";
	private static final int PAGER_ID = 17;
	private static final String PAGE_POSITION = "page_position";
	private static final String IMAGE_SELECTION_MODE_ENABLED = "image_selection_mode_enabled";
	
	private ViewPager mViewPager;
	private boolean mImageSelectionModeEnabled;
	private Activity mActivity;
	private int mPagePosition;
	
	private int mSavedActivityId = -1;
	
	private SendActivityFragment mSendActivityFragment;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setupSoftInputModeCompat();
		if (savedInstanceState != null) {
			mPagePosition = savedInstanceState.getInt(PAGE_POSITION);
			mImageSelectionModeEnabled = savedInstanceState.getBoolean(IMAGE_SELECTION_MODE_ENABLED);
			mSavedActivityId = savedInstanceState.getInt(ACTIVITY_ID);
		}
		mViewPager = new ViewPager(this);
		mViewPager.setId(PAGER_ID);
		mViewPager.setAdapter(new ThreePageAdapter(getSupportFragmentManager()));
		mViewPager.setOnPageChangeListener(new SimpleOnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				if (position != mPagePosition) {
					mPagePosition = position;
					getSupportActionBar().getTabAt(position).select();
				}
			}
		});
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		setContentView(mViewPager);
		setupTabs();
		mActivity = findOrCreateActivity();
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupSoftInputModeCompat() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
					| 
					WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
		} else {
			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		}
	}
	
	private void setupTabs() {
		TabListener tabListener = new SimpleTabListener() {
			@Override
			public void onTabSelected(Tab tab, FragmentTransaction ft) {
				if (mViewPager != null) {
					mViewPager.setCurrentItem(tab.getPosition());
				}
			}
		};
		ActionBar ab = getSupportActionBar();
		ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		Tab detailsTab = ab.newTab().setText(R.string.details).setTabListener(tabListener);
		Tab imagesTab = ab.newTab().setText(R.string.images).setTabListener(tabListener);
		Tab mapTab = ab.newTab().setText(R.string.map).setTabListener(tabListener);
		ab.addTab(detailsTab);
		ab.addTab(imagesTab);
		ab.addTab(mapTab);
		if (mPagePosition != -1) {
			// if state is being restored
			ab.getTabAt(mPagePosition).select();
		} else {
			ab.getTabAt(0).select();
		}
	}
	
	public Activity getActivityModel() {
		return mActivity;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (!mImageSelectionModeEnabled) {
			getSupportMenuInflater().inflate(R.menu.activity, menu);
			return true;
		}
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
			switch (item.getItemId()) {
			case R.id.send:
				sendActivity();
				break;
			default:
				break;
			}
		return super.onOptionsItemSelected(item);
	}
	
	private void sendActivity() {
		if (mSendActivityFragment != null && mSendActivityFragment.isInProgress()) {
			return;
		}
		
		List<Fragment> fragments = getSupportFragmentManager().getFragments();
		for (Fragment f : fragments) {
			if (f instanceof IBaseActivityFragment) {
				((IBaseActivityFragment) f).saveActivity(mActivity);
			}
		}
		
		mSendActivityFragment = new SendActivityFragment();
		mSendActivityFragment.beginSend(getSupportFragmentManager(), mActivity);
	}

	public void setImageSelectionModeEnabled(boolean value) {
		if (mImageSelectionModeEnabled != value) {
			mImageSelectionModeEnabled = value;
			invalidateActionBar();
		}
	}
	
	public boolean isImageSelectionModeEnabled() {
		return mImageSelectionModeEnabled;
	}

	private Activity findOrCreateActivity() {
		RuntimeExceptionDao<Activity, Integer> dao = getHelper().getActivityDao();
		Activity activity = dao.queryForId(mSavedActivityId);
		if (activity == null) {
			activity = new Activity();
			dao.create(activity);
		}
		return activity;
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public void invalidateActionBar() {
		invalidateOptionsMenu();
	}
	
	public boolean wasActivitySent() {
		return mSendActivityFragment != null ? mSendActivityFragment.wasSent() : false;
	}
	
	@Override
	protected void onPause() {
		if (isFinishing() && !wasActivitySent()) {
			mActivity.setStatus(Status.DRAFT);
		}
		getHelper().getActivityDao().update(mActivity);
		super.onPause();
	}
	
	@Override
	public void onBackPressed() {
		if (mImageSelectionModeEnabled) {
			setImageSelectionModeEnabled(false);
		} else {
			super.onBackPressed();
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(PAGE_POSITION, mPagePosition);
		outState.putBoolean(IMAGE_SELECTION_MODE_ENABLED, mImageSelectionModeEnabled);
		outState.putInt(ACTIVITY_ID, mActivity.getId());
	}
	
}
